package tests.create;

import static org.junit.Assert.fail;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

/*
 * 
 * Attempts to create a budget with 10 lines
 * 
 */

public class LineCreateTest01
{
	Budget budget;
	BudgetFactory factory;
	Line line;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		 factory = new BudgetFactory();
		 budget = (Budget) factory.create();
         budget.setName("Test budget");
         budget.setDescription("This is a test budget.");
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete( (Line)line, "Lines", WorkletContext.getInstance());
		Helper.delete( (Budget)budget, "Budgets", WorkletContext.getInstance());
    }

	@Test
	public void test()
	{
		try
        {
			for(int i = 0; i < 10; i++)
			{
				Line line = (Line) budget.createLine();
				budget.add(line);
		
				Boolean successful = budget.commit();
				
				if(!successful)
				{
					fail("Could not create/add line");
				}
			}
        }
		catch(Exception e)
		{
			 fail("Lines create failed with exception " + e);
		}
		
		System.out.println("Lines commited. LineCreateTest01.java passed.");
	}
}